#include "ui_checkbox.h"
#include "ui_manager.h"

#include "audio/audio.h"

#include "util/timer.h"		    // Needs to come in early
#include "util/mmgr.h"		    // Needs to come in early
#include "util/common_macros.h"		// macros
#include "util/log.h"		    // logging

#include "script/scripthelper.h"
#include "script/write_lua.h"
#ifdef _ENABLE_LUABIND
#include <luabind/luabind.hpp>
#endif // _ENABLE_LUABIND
using namespace ui;
using namespace script;


UICheckBox::UICheckBox(UIWidget* pParent,const std::string& Name) :
	UIWidget(pParent, Name),
	m_IsChecked(false)
{
	m_Type = WT_CHECKBOX;
}

UICheckBox::~UICheckBox()
{

}

bool		UICheckBox::Create(const std::string& Name, const math::Vec2& Pos, const math::Vec2& Size)
{
	UIWidget::Create(Name, Pos, Size);
	// create progress stuff

	return true;
}

bool		UICheckBox::Load(LuaPlus::LuaObject& _LObj)
{
	UIWidget::Load(_LObj);	// get all the common stuff loaded
	SetChecked(ScriptHelper::GetTableBool(_LObj, "IsChecked", false));
	std::string UncheckedImg = ScriptHelper::GetTableString(_LObj, "UncheckedImg", "");
	std::string CheckedImg   = ScriptHelper::GetTableString(_LObj, "CheckedImg", "");
	m_ImageA[WI_CHECKED].Create(CheckedImg);
	m_ImageA[WI_UNCHECKED].Create(UncheckedImg);

	return true;
}
bool		UICheckBox::ExportSpecificsToFile(script::WriteLua*	pWL)
{
	pWL->WritePair("IsChecked", IsChecked());
	pWL->WritePair("UncheckedImg", m_ImageA[WI_UNCHECKED].GetFilename());
	pWL->WritePair("CheckedImg", m_ImageA[WI_CHECKED].GetFilename());
	return true;
}


bool		UICheckBox::Tick(float dt)
{
	UIWidget::Tick(dt);	// tick children, etc
	m_ImageA[WI_UNCHECKED].Tick(dt);
	m_ImageA[WI_CHECKED].Tick(dt);
	return true;
}

void		UICheckBox::SetChecked(bool b)
{
	m_IsChecked = b;
//	TriggerEvent(WE_TEXT_SELECTED);
}


bool		UICheckBox::OnMouseRelease(const math::Vec2& Pos, unsigned int Button)
{
	/// assume that all children are inside the volume of the parent?
	if (!m_IsActive || !m_IsVisible || !IsInside(Pos))
	{
		return false;
	}

	SetChecked(!IsChecked());
	AUDIO().PlayEvent("button_press");


	for (ElementList::iterator It = m_Set.begin(); It != m_Set.end(); It++)
	{
		if (It->second->OnMouseDown(Pos, Button))
			return true;
	}

	TriggerEvent(WE_LMB_RELEASE);
	return false;
}

bool		UICheckBox::Render(bool Force)
{
	if (!Force)
	{
		// look for things that could disqualify this one from being rendered
		if (!m_IsVisible)
			return false;	// disable entire widget
		if (m_pManager->GetDragTarget() == this)
			return false;	// will get rendered on top anyways
	}
	RenderHooverTip();
	if (m_pManager) m_pManager->WidgetRendered();

	if (m_IsChecked)
	{
		m_ImageA[WI_CHECKED].Setup(m_Pos, m_Size, m_Rotation, m_Scale, m_Color);
		m_ImageA[WI_CHECKED].Render();
	} else
	{
		m_ImageA[WI_UNCHECKED].Setup(m_Pos, m_Size, m_Rotation, m_Scale, m_Color);
		m_ImageA[WI_UNCHECKED].Render();
	}

	for (ElementList::iterator It = m_Set.begin(); It != m_Set.end(); It++)
		It->second->Render();

	return true;
}

UIWidget*	UICheckBox::GetClone()
{
	UICheckBox*	pNew = new UICheckBox(m_pParent, GetCloneName(m_Name));
	FillCloneInfo((UIWidget*)pNew);
	pNew->m_IsChecked = this->m_IsChecked;
	return pNew;
}


/// register with LUA Bind
void	UICheckBox::Register(LuaPlus::LuaState* _pScript)
{
#ifdef _ENABLE_LUABIND
    lua_State * L = _pScript->GetCState ( );

	luabind::module(L)
    [

		luabind::class_<UICheckBox, UIWidget >( "SliderWidget" )
		.def( "IsChecked",			&UICheckBox::IsChecked)
		.def( "SetChecked",			&UICheckBox::SetChecked)
	];
#endif //_ENABLE_LUABIND
}
